package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.manage.config.Config;
import com.manage.model.AppPointOfBilling;
import com.manage.model.PointSdkConfig;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class PointSdkConfigServiceImpl extends AbstractService implements
		PointSdkConfigService {
	
	private Logger logger;
	
	public PointSdkConfigServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	@Override
	public List<PointSdkConfig> getPointSdkConfigByPointofbillingid(
			int pointofbillingid) {
		String sql = "select * from pointsdkconfig where pointofbillingid='"+pointofbillingid+"' order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<PointSdkConfig> dbcpmg = new ModelGenerator<PointSdkConfig>(PointSdkConfig.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean savePointSdkConfig(List<PointSdkConfig> list,int pointofbillingid) {
		String [] sql = new String [list.size()+1];
		sql[0] = "delete from pointsdkconfig where pointofbillingid = '"+pointofbillingid+"'";
		for(int i =0;i<list.size();i++)
		{
			PointSdkConfig model = list.get(i);
			model.setId(getTableId("pointsdkconfig"));
			sql[i+1] = "insert into pointsdkconfig(id,cpid,appnumber,pointofbillingid,sdkprovidersid,prarmatervalue) values " +
					" ('"+model.getId()+"','"+model.getCpid()+"','"+model.getAppnumber()+
					"','"+model.getPointofbillingid()+"','"+model.getSdkprovidersid()+"','"+model.getPrarmatervalue()+"')";
			
		}
		return ConnectionManager.getInstance().executeBatch(dbcp, sql);
	}
	@Override
	public List<PointSdkConfig> getPointSdkConfigs() {
		String sql = "select * from pointsdkconfig order by id";
		ModelGenerator<PointSdkConfig> dbcpmg = new ModelGenerator<PointSdkConfig>(PointSdkConfig.class);
		return dbcpmg.generateList(dbcp, sql);
	}

}
